1
Easy2Siksha
GNDU Queson Paper - 2021
Bachelor of Computer Applicaon (BCA) 5st Semester
JAVA PROGRAMMING LANGUAGE
Paper-IV
Time Allowed – 3 Hours Maximum Marks-75
Note :- Aempt FIVE Queson in all, selecng at least ONE queson from each secon . The FIFTH
Queson may be aempted from any Secon . All Quesons carry equal marks .
SECTION-A
1.Which are various data types of Java ? what is dierence between keyword, idener, literal and
operator ? Give example of each.
2.(a) Explain Object Oriented features of Java.
(b) What is dierence between class and object ? Give example.
SECTION-B
3.What is use of method overriding and method overloading ? Give an example of both. What is
use of super and nal keywords ?
4.What is Interface ? What are various similaries and dierences between class and Interface?
SECTION-C
5. Explain the use of the following in Java :
(a) try-catch
(b) try-nally
(c) throw and throws
6. What is use of threads ? Explain the thread life cycle.
SECTION-D
2
Easy2Siksha
7. Explain the following :
(a) Buered Input / Output stream
(b) JDBC
8. Write a program in java to show records of employee table of My SQL . Assume table as :
Employee (EmpID, EmpName, Salery, DOJ)
Gndu Answer Paper 2021
Bachelor of Computer Applicaon (BCA) 5st Semester
JAVA PROGRAMMING LANGUAGE
Paper-IV
SECTION-A
1.Which are various data types of Java ? what is dierence between keyword, idener,
literal and operator ? Give example of each.
Ans: In Java, there are several data types that help dene and categorize the type of data a
variable can hold. Let's explore these data types and clarify the dierences between
keywords, ideners, literals, and operators:
**Data Types in Java:**
1. **Primive Data Types:**
- These are the basic data types in Java that represent simple values.
- Examples:
- `int`: Used for whole numbers, e.g., `int age = 30;`
- `double`: Used for oang-point numbers, e.g., `double price = 19.99;`
- `boolean`: Used for true or false values, e.g., `boolean isReady = true;`
- `char`: Used for single characters, e.g., `char grade = 'A';`
3
Easy2Siksha
2. **Reference Data Types:**
- These data types are more complex and store references to objects.
- Examples:
- `String`: Used for storing text, e.g., `String name = "John";`
- `Object`: The most generic type that can refer to any object, e.g., `Object person = new
Person();`
**Dierences:**
1. **Keyword**:
- Keywords are reserved words in Java that have specic meanings and purposes.
- They cannot be used as ideners (variable names).
- Example: `public`, `class`, `if`, `return`.
2. **Idener**:
- Ideners are names used for variables, methods, classes, and other user-dened
elements.
- They should start with a leer, underscore, or dollar sign, followed by leers, numbers,
underscores, or dollar signs.
- Example: `myVariable`, `calculateTotal`, `MyClass`.
3. **Literal**:
- Literals represent constant values in Java.
- They can be of dierent types, such as numeric literals (`10`, `3.14`), character literals
(`'A'`, `'1'`), string literals (`"Hello"`), and boolean literals (`true`, `false`).
4. **Operator**:
- Operators are symbols used to perform operaons on data.
- They can be arithmec operators (`+`, `-`, `*`), comparison operators (`>`, `<`, `==`), logical
operators (`&&`, `||`), and more.
4
Easy2Siksha
- Example: `x + y`, `age > 18`, `isReady && isAcve`.
In summary, Java provides a variety of data types to work with dierent types of data.
Keywords are reserved words with specic meanings, ideners are names for user-dened
elements, literals represent constant values, and operators are symbols used for various
operaons. Understanding the disncons between these concepts is essenal for eecve
Java programming.
2.(a) Explain Object Oriented features of Java
Ans: Certainly! Java is an object-oriented programming language, which means it's designed
to work with objects, allowing you to structure your code in a way that's more organized and
easier to manage. Here, I'll explain some key object-oriented features of Java with examples:
1. **Classes and Objects**:
- **Explanaon**: In Java, everything is based on classes and objects. A class is like a
blueprint, and an object is an instance of that blueprint.
- **Example**:
2. **Encapsulaon**:
- **Explanaon**: Encapsulaon is about bundling data (aributes) and methods
(funcons) that operate on the data into a single unit (class). This helps in data hiding and
access control.
- **Example**:
5
Easy2Siksha
3. **Inheritance**:
- **Explanaon**: Inheritance allows a new class (subclass or child class) to inherit
properes and behaviors from an exisng class (superclass or parent class).
- **Example**:
4. **Polymorphism**:
- **Explanaon**: Polymorphism allows objects of dierent classes to be treated as
objects of a common superclass. It enables method overriding and dynamic method
invocaon.
- **Example**:
5. **Abstracon**:
6
Easy2Siksha
- **Explanaon**: Abstracon is the process of simplifying complex reality by modeling
classes based on what an object does rather than how it does it.
- **Example**:
In summary, Java's object-oriented features help developers build modular, maintainable,
and reusable code. Classes and objects are the building blocks, encapsulaon ensures data
security, inheritance promotes code reuse, polymorphism enables exibility, and abstracon
simplies complex systems. These concepts make Java a powerful and widely used
programming language.
(b) What is dierence between class and object ? Give example.
Ans: Certainly! Let's explore the dierence between a class and an object .
**Class**:
A class is like a blueprint or a template for creang objects. It denes the characteriscs and
behaviors that objects of a parcular type will have. Think of a class as a set of instrucons
for building something. It doesn't exist in the real world but serves as a plan for creang
objects.
**Object**:
An object, on the other hand, is a specic instance created from a class. It's like the actual
thing built using the blueprint. Objects have their own unique properes and can perform
acons dened in the class. You can have mulple objects created from the same class, each
with its own disnct aributes and behavior.
**Example**:
Let's use a simple analogy to illustrate the dierence. Imagine you have a class called "Car."
7
Easy2Siksha
- **Class (Car Blueprint)**: The "Car" class denes what a car is. It species that a car has
properes like color, make, and model, and it can perform acons like starng, stopping, and
honking. But the class itself doesn't represent any specic car; it's just a descripon of what
a car should be.
- **Objects (Actual Cars)**: Objects are instances of the "Car" class. You can create real cars
from the blueprint. For instance, you might create two objects: one represenng a red Ford
Mustang and another represenng a blue Toyota Camry. Each of these objects will have its
own color, make, model, and can be driven independently.
In summary, a class is the plan or blueprint, and an object is the real thing created based on
that plan. Classes allow us to dene the structure and behavior of objects, making it easier
to create and manage dierent instances of the same type.
SECTION-B
3.What is use of method overriding and method overloading ? Give an example of both.
What is use of super and nal keywords ?
Ans: **Method Overloading** and **Method Overriding** are two fundamental concepts
in object-oriented programming, oen used in languages like Java or C++.
**Method Overloading**:
Method overloading is when you have mulple methods in the same class with the same
name but dierent parameters. It's used to create methods that perform similar tasks but
can handle dierent types of input. This way, you don't need to create a dierent method for
each case. Here's an example:
8
Easy2Siksha
In this example, we have two `add` methods with dierent parameter types (int and double).
This allows you to add both integers and oang-point numbers using the same method
name.
**Method Overriding**:
Method overriding is used when you have a method in a parent class, and you want to
provide a specic implementaon of that method in a child class. It's a way to change or
extend the behavior of the inherited method. For instance:
Here, the `draw` method in the `Shape` class is overridden in the `Circle` class to provide a
specialized implementaon for drawing a circle.
**super Keyword**:
The `super` keyword is used to call a method or access a variable from the parent class. It's
handy when you're overriding a method in a child class but sll want to use the funconality
of the parent class. For example:
**nal Keyword**:
9
Easy2Siksha
The `nal` keyword is used to prevent further modicaon of a class, method, or variable.
When a class is declared as `nal`, it cannot be extended (no inheritance). When a method is
marked as `nal`, it can't be overridden by child classes. For example:
In summary, method overloading and overriding allow you to create exible and specialized
code, while the `super` keyword lets you access parent class elements in child classes, and
the `nal` keyword restricts further modicaons to classes or methods.
4.What is Interface ? What are various similaries and dierences between class and
Interface?
Ans: An interface is a concept in programming that denes a contract for how classes should
behave. It sets up a list of methods that must be implemented by any class that claims to
conform to the interface. In simpler terms, an interface is like a blueprint for classes, telling
them what methods they need to have and how they should work. Here's an easy-to-
understand explanaon of the similaries and dierences between classes and interfaces:
**Similaries**:
1. **Methods**: Both classes and interfaces can have methods. A method is a block of code
that performs a specic task. Classes can have their methods, and interfaces can declare
methods that classes must implement.
2. **Polymorphism**: Both classes and interfaces contribute to polymorphism, a
fundamental concept in object-oriented programming. Polymorphism allows objects of
dierent classes to be treated as objects of a common interface. This means you can use
objects in a generic way, making your code more exible and adaptable.
10
Easy2Siksha
**Dierences**:
1. **Implementaon**: The most signicant dierence is that classes can contain both
method declaraons and method implementaons. In contrast, an interface can only declare
method signatures (names, parameters, and return types) but can't provide the actual code
for those methods.
2. **Inheritance**: Classes can extend (inherit from) other classes, inhering their
properes and methods. However, a class can only inherit from one class (single
inheritance). In contrast, a class can implement mulple interfaces. This allows a class to
promise that it will provide the behavior specied in mulple interfaces.
3. **Constructor**: Classes have constructors, which are special methods used to create
and inialize objects. Interfaces can't have constructors because they don't create objects
themselves; they dene a set of rules for how objects should behave.
4. **Variables**: Classes can have instance variables (also called elds or properes) that
store data. Interfaces can't have instance variables; they dene the structure and behavior
but don't store data.
In summary, a class is like a blueprint for creang objects, and it can provide the actual
implementaon of methods. An interface, on the other hand, is like a contract that classes
promise to follow, specifying what methods they should have without providing the method
implementaons. While classes enable code reusability through inheritance, interfaces allow
classes to support mulple contracts, enabling greater exibility in designing and structuring
your code.
SECTION-C
5. Explain the use of the following in Java :
(a) try-catch
(b) try-nally
(c) throw and throws
Ans: (a) **try-catch**:
- In Java, `try` and `catch` are used together to handle excepons. When you put a block of
code inside a `try` block, you're telling Java to monitor for errors within that code. If an error
11
Easy2Siksha
occurs, it's caught by the `catch` block, prevenng your program from crashing. This is
essenal for robust programming because it allows you to gracefully handle unexpected
issues. For instance, if you're reading a le and it doesn't exist, you can catch the
"FileNotFound" excepon and display a user-friendly error message instead of leng the
program crash. In easy terms, think of "try-catch" as a safety net for your code.
(b) **try-nally**:
- The `try-nally` block is used to ensure that a specic piece of code (usually clean-up or
resource release code) is always executed, whether an excepon is thrown or not. For
instance, if you open a le or a database connecon inside a `try` block, you can use `try-
nally` to guarantee that the le is closed or the database connecon is properly
terminated, even if an excepon occurs. It's like making sure you turn o the lights and lock
the door when leaving a room, regardless of what happens while you're in there.
(c) **throw and throws**:
- In Java, `throw` is used to explicitly create and throw an excepon when a parcular
condion is met. This is useful when you want to signal that something unexpected has
happened in your code. For example, you might `throw` a custom excepon if a user tries to
log in with an incorrect password. On the other hand, `throws` is used in a method signature
to indicate that the method might throw an excepon. It doesn't handle the excepon; it
simply informs the calling code that it should be prepared to catch an excepon that might
be thrown by the method. It's like pung a "Cauon: Wet Floor" sign, warning others about
a potenal hazard.
In summary, these constructs in Java help you deal with unexpected situaons, ensure
proper resource management, and communicate the possibility of excepons to other parts
of your code. They are crucial for wring reliable and robust Java programs.
6. What is use of threads ? Explain the thread life cycle.
Ans: **Threads** are a crucial part of computer programs that help them run smoothly and
eciently. Think of them as lile workers inside your computer, each handling a specic
task.
**Thread Life Cycle**:
12
Easy2Siksha
1. **New**: A thread begins its life in the "New" state. It's like a new employee who's just
been hired. In this stage, the thread is created but hasn't started running yet.
2. **Runnable**: When the thread is ready to work, it moves to the "Runnable" state. Just
like a worker waing for their turn, a runnable thread is in the queue to be executed.
3. **Running**: The thread enters the "Running" state when it's acvely execung its tasks.
Imagine this as the worker doing their job. In a mul-core processor, mulple threads can be
in the running state simultaneously.
4. **Blocked/Waing**: Somemes, a thread can't connue its work, like when it's waing
for some data or a resource. In this state, it's "Blocked" or "Waing" for the necessary
condions to proceed.
5. **Terminated**: When a thread completes its task or is no longer needed, it enters the
"Terminated" state. This is like an employee who nishes their work and leaves the company.
6. **Dead**: Once a thread is terminated, it's considered "Dead." It can't be restarted or
moved to another state. It's like a former employee who's no longer part of the company.
Threads are incredibly useful in programming because they allow mulple tasks to be
executed concurrently. This means your computer can do several things at once, like playing
music while browsing the internet or running background tasks while you're using an
applicaon. However, managing threads can be complex, and programmers need to ensure
they work together without causing problems, like data conicts or crashes. So, threads are
like the workforce of your computer, making sure everything runs eciently and smoothly.
SECTION-D
7. Explain the following :
(a) Buered Input / Output stream
Ans: Buered Input/Output Stream
13
Easy2Siksha
Buered Input and Output streams are concepts in computer programming that help
improve the eciency of reading and wring data to and from les or other sources like
network connecons. Imagine you are pouring water from a large container into smaller
cups. You can either pour one drop at a me (unbuered), or you can pour a bit into a
smaller container and then pour from that smaller container into the cups (buered).
Buered streams work somewhat like that smaller container, making the process more
ecient.
**Buered Input Stream**:
When you read data from a le or some other source, it's oen done in small pieces. Each
me you read a piece, your program may need to interact with the le system or network,
which can be slow. Buered Input Streams, however, read data in larger chunks and store it
in a temporary memory space (the buer) before your program uses it. This reduces the
number of interacons with the le system or network, making the reading process faster.
It's like collecng a bunch of puzzle pieces before assembling them, rather than geng one
piece at a me.
**Buered Output Stream**:
Similarly, when you write data to a le or send it over a network, each small piece may
trigger a separate interacon with the output desnaon, which can also be slow. Buered
Output Streams collect a poron of your data in the buer and then write it all at once,
reducing the number of interacons and making wring more ecient. It's like pouring
mulple cups of water from your smaller container in one go, rather than one cup at a me.
In essence, buered streams act as intermediaries that help reduce the me your program
spends interacng with the le system or network, which speeds up the process of reading
and wring data. They are a fundamental part of opmizing le I/O operaons in soware
development.
(b) JDBC
Ans: JDBC, which stands for Java Database Connecvity, is a technology that allows Java
programs to interact with databases. It's like a bridge between the world of Java and the
world of databases, making it easy for Java applicaons to retrieve, store, and manipulate
data in a database.
Here's a simple breakdown of what JDBC does and why it's important:
14
Easy2Siksha
1. **Database Interacon**: JDBC enables Java applicaons to connect to a wide range of
databases, such as MySQL, Oracle, or PostgreSQL. It acts as a communicator that
understands the language of both Java and the specic database system.
2. **Database Access**: Once connected, Java applicaons can send SQL (Structured Query
Language) statements to the database using JDBC. SQL statements are like commands that
tell the database what to do, such as retrieving specic data or updang records.
3. **Data Retrieval**: With JDBC, you can fetch data from a database and use it in your Java
program. For example, if you have an e-commerce applicaon, you can use JDBC to retrieve
product informaon, like names, prices, and descripons from the database and display
them on your website.
4. **Data Modicaon**: JDBC also allows you to insert, update, and delete data in the
database. If a customer places an order, JDBC can be used to store this informaon in the
database for future reference.
5. **Security**: JDBC provides features for secure database interacons. You can control
who can access the database and what they can do, ensuring that sensive data is
protected.
6. **Portability**: JDBC is plaorm-independent, which means you can write your Java
applicaon with JDBC, and it will work with dierent databases without major modicaons.
This makes it versale and saves me when switching databases or plaorms.
In essence, JDBC is a crical tool for developers because it simplies the process of
integrang databases with Java applicaons. It empowers soware to store, retrieve, and
manage data, making it essenal for any applicaon that needs to work with data stored in a
database system.
8.Write a program in java to show records of employee table of My SQL . Assume table as :
Employee (EmpID, EmpName, Salery, DOJ)
Ans: Certainly! To show records from an Employee table in a MySQL database using Java,
you can follow these easy-to-understand steps. First, ensure you have the MySQL
Connector/J library in your Java project to interact with the database.
15
Easy2Siksha
Remember to replace `yourDatabaseName`, `yourUsername`, and `yourPassword` with your
actual database details. This Java program connects to the MySQL database, retrieves
records from the Employee table, and prints them to the console. It uses JDBC (Java
Database Connecvity) to establish the connecon and execute the SQL query.
Please ensure that you have the MySQL Connector/J library included in your project, and the
MySQL server is up and running. Also, handle excepons and close the connecons properly
for robust code.
Note: This Answer Paper is totally Solved by Ai (Arcial Intelligence) So if You nd Any Error Or
Mistake . Give us a Feedback related Error , We will Denitely Try To solve this Problem Or Error.